/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 * $LastChangedRevision: 2290 $
 */ 

/** @file
 * Implementatio of Hardware Abstraction Layer for the USB controller.
 *
 * This file contain:
 * - the USB-controller register layout
 * - the USB-controller interrupts towards the MCU
 *
 * The usb_map_t structure is set to point at xdata address 0x0000
 *
 * @author Ken A. Redergaard
 */

#ifndef __USB_MAP_H__
#define __USB_MAP_H__

#include <stdint.h>

#define USB_BASE_ADDRESS 0xC440      // The USB-controller registers start at 0xC440
#define USB_EP_DEFAULT_BUF_SIZE 0x20 // (32)

#define USB_WU  11 // Keil sees interrupt 11 at address 0x005b
#define USB_IRQ 12 // Keil sees interrupt 12 at address 0x0063

#define RESERVED(res_nr, size)  \
     volatile uint8_t reserved ## res_nr[size]

typedef struct {
     RESERVED(0, USB_BASE_ADDRESS);
     volatile uint8_t out5buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(1, 0x21 - 1);
     volatile uint8_t in5buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(2, 0x21 - 1);
     volatile uint8_t out4buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(3, 0x21 - 1);
     volatile uint8_t in4buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(4, 0x21 - 1);
     volatile uint8_t out3buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(5, 0x21 - 1);
     volatile uint8_t in3buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(6, 0x21 - 1);
     volatile uint8_t out2buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(7, 0x21 - 1);
     volatile uint8_t in2buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(8, 0x21 - 1);
     volatile uint8_t out1buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(9, 0x21 - 1);
     volatile uint8_t in1buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(10, 0x21 - 1);
     volatile uint8_t out0buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(11, 0x21 - 1);
     volatile uint8_t in0buf[USB_EP_DEFAULT_BUF_SIZE];
     RESERVED(12, 0x41 - 1);
     volatile uint8_t out8data;
     RESERVED(13, 0x08 - 1);
     volatile uint8_t in8data;
     RESERVED(14, 0x08 - 1);
     volatile uint8_t out8bch;
     volatile uint8_t out8bcl;
     RESERVED(15, 0x10 - 1);
     volatile uint8_t bout1addr;
     volatile uint8_t bout2addr;
     volatile uint8_t bout3addr;
     volatile uint8_t bout4addr;
     volatile uint8_t bout5addr;
     RESERVED(16, 0x03 - 1);
     volatile uint8_t binstaddr;
     volatile uint8_t bin1addr;
     volatile uint8_t bin2addr;
     volatile uint8_t bin3addr;
     volatile uint8_t bin4addr;
     volatile uint8_t bin5addr;
     RESERVED(17, 0x13 - 1);
     volatile uint8_t isoerr;
     RESERVED(18, 0x02 -1);
     volatile uint8_t zbcout;
     RESERVED(19, 0x06 - 1);
     volatile uint8_t ivec;

     volatile uint8_t in_irq;
     volatile uint8_t out_irq;
     volatile uint8_t usbirq;
  	
     volatile uint8_t in_ien;
     volatile uint8_t out_ien;
     volatile uint8_t usbien;
     volatile uint8_t usbbav;
     RESERVED(20, 0x05 - 1);
     volatile uint8_t ep0cs;
     volatile uint8_t in0bc;

     volatile uint8_t in1cs;
     volatile uint8_t in1bc;
  
     volatile uint8_t in2cs;
     volatile uint8_t in2bc;

     volatile uint8_t in3cs;
     volatile uint8_t in3bc;

     volatile uint8_t in4cs;
     volatile uint8_t in4bc;

     volatile uint8_t in5cs;
     volatile uint8_t in5bc;
     RESERVED(21, 0x06 - 1);
     volatile uint8_t out0bc;
     volatile uint8_t out1cs;
     volatile uint8_t out1bc;

     volatile uint8_t out2cs;
     volatile uint8_t out2bc;

     volatile uint8_t out3cs;
     volatile uint8_t out3bc;

     volatile uint8_t out4cs;
     volatile uint8_t out4bc;

     volatile uint8_t out5cs;
     volatile uint8_t out5bc;
     RESERVED(22, 0x07 - 1);
     volatile uint8_t usbcs;
     volatile uint8_t togctl;
     volatile uint8_t usbfrml;
     volatile uint8_t usbfrmh;
     RESERVED(23, 0x02 - 1);
     volatile uint8_t fnaddr;
     RESERVED(24, 0x02 - 1);
     volatile uint8_t usbpair;
     volatile uint8_t inbulkval;
     volatile uint8_t outbulkval;
     volatile uint8_t inisoval;
     volatile uint8_t outisoval;
     RESERVED(25, 0x07 - 1);
     volatile uint8_t setupbuf[8];

     volatile uint8_t out8addr;
     RESERVED(26, 0x08 - 1);
     volatile uint8_t in8addr;
} usb_map_t;
 
#endif // __USB_MAP_H__
